package com.kbt.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;
import com.android.volley.Request;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.BaseBean;
import com.kbt.net.RequestManager;
import com.kbt.util.data.StaticData;
import com.kbt.util.utils.UpdateManager;
import com.kbt.util.utils.Utils;

import java.util.HashMap;
import java.util.Map;

public class WelcomeActivity extends Activity implements RequestManager.ResponseInterface{

    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private BaseBean baseBean;
    private Handler welcomePageHandler = new Handler() {
      @Override
        public void handleMessage(Message msg) {

            if (msg.what == StaticData.WELCOME_CLOSE_FLAG) {
                Intent intent = new Intent(WelcomeActivity.this, StoreActivity.class);
                startActivity(intent);
                BaseApplication.getInstance().removeActivity(WelcomeActivity.this);
                finish();
            }

        }

    };

    /**
     * 主界面程序
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        /*******
         //自义标题栏
         //        super.onCreate(savedInstanceState);
         //        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
         //        setContentView(R.layout.welcome_layout);
         //        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.application_title_layout);
         *****/

        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();

        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_layout);

//        //是否更新
//        httpVersion();
    }
    //是否更新
    private void httpVersion(){
        if(Utils.isNetworkAvailable(WelcomeActivity.this)){
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectVersionUrl, Constants.RequestTag.selectVersionTag, map, BaseBean.class);
        }else{
            Toast.makeText(WelcomeActivity.this, R.string.Wangluo, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        //3秒后关闭欢迎页
        Message welcomeMsg = new Message();
        welcomeMsg.what = StaticData.WELCOME_CLOSE_FLAG;
        welcomePageHandler.sendMessageDelayed(welcomeMsg, 1 * 1000);
        BaseApplication.getInstance().addActivity(WelcomeActivity.this);
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.selectVersionTag) {
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                // 判断是否是有数据
                if (baseBean.getResult().equals("success")) {// 成功
                    int versionCode = getVersionCode(WelcomeActivity.this);
                    if(versionCode<Integer.parseInt(baseBean.getMsg())) {
                        UpdateManager updatemanager = new UpdateManager(WelcomeActivity.this);
                        updatemanager.showNoticeDialog();
                    }
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {

    }
    /**
     * 获取软件版本号
     *
     * @param context
     * @return
     */
    private int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            // 获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCode = context.getPackageManager().getPackageInfo("com.kbt.activity", 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }
}
